package com.hqyj.cl.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.cl.pojo.User;
import com.hqyj.cl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller//表名这个类是一个控制器
@RequestMapping("/user")//类路径
public class UserController {
    //自动装配
    @Autowired
    private UserService userService;
    //login
    @RequestMapping("/login")
    public String login(String username, String password){
        //返回
        return userService.login(username, password);
    }
    //selectAllUser
    @RequestMapping("/selectAllUser")
    @ResponseBody
    public PageInfo<User> selectAllUser(int pageNum, int pageSize){
        if(pageNum ==-1){
            pageNum=1;
        }
        return userService.selectAllUser(pageNum, pageSize);
    }

    @RequestMapping("/deleteUserById")
    @ResponseBody
    public Map<String, String> deleteUserById(int id){
        return userService.deleteUserById(id);
    }

    @RequestMapping("/selectUserByUserName")
    @ResponseBody
    public PageInfo<User> selectUserByUserName(int pageNum, int pageSize,String username){
        if(pageNum ==-1){
            pageNum=1;
        }
        return userService.selectUserByUserName(pageNum, pageSize,username);
    }
    @RequestMapping("/updateUserByUsername")
    @ResponseBody
    public Map<String, String> updateUserByUsername(User user){
        return userService.updateUserByUsername(user);
    }
    @RequestMapping("/addUser")
    @ResponseBody
    public Map<String, String> addUser(User user){
        return userService.addUser(user);
    }

    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String, String> checkUsername(String username){
        return userService.checkUsername(username);
    }
}
